package com.sgcc.ln.pmos.e_ln_mall.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.common.base.MvpBaseFragment;
import com.sgcc.ln.pmos.common.bean.PackageBean;
import com.sgcc.ln.pmos.common.utils.DateUtils;
import com.sgcc.ln.pmos.e_ln_mall.bean.DataSelect;
import com.sgcc.ln.pmos.e_ln_mall.bean.OrderCheckResutBean;
import com.sgcc.ln.pmos.e_ln_mall.contract.OrderTimeChoiceContract;
import com.sgcc.ln.pmos.e_ln_mall.presenter.OrderTimeChoicePresenter;

import java.util.ArrayList;
import java.util.List;


//立即下单页面中时间端选择的模块页面
public class OrderDateChioceFragment_old extends MvpBaseFragment<OrderTimeChoicePresenter> implements OrderTimeChoiceContract.View {
    TextView next_txt;
    TextView start_time, end_time;

    List<Integer> yearList = new ArrayList<>();
    List<List<Integer>> monthList = new ArrayList<>();
    List<List<Integer>> seasonList = new ArrayList<>();
    List<List<String>> seasonStrList = new ArrayList<>();


    List<Integer> yearListEnd = new ArrayList<>();
    List<List<Integer>> monthListEnd = new ArrayList<>();
    List<List<Integer>> seasonListEnd = new ArrayList<>();
    List<List<String>> seasonStrListEnd = new ArrayList<>();

    String dateType = "month";//season
    int sysStartYear = 0;
    int sysStartMonth = 0;
    int sysStartSeason = 0;

    int sysEndYear = 0;
    int sysEndMonth = 0;
    int sysEndSeason = 0;

    int userStartYear = 0;
    int userStartMonth = 0;
    int userStartSeason = 0;

    int nowMouth = 0;
    int nowYear = 0;
    boolean isStartDataSelect = true;
    private int dataSelectType = 0;


    private OnTimeSelectListener onTimeSelectListener;
    PackageBean packageBean;
    DataSelect dataSelect;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_order_date_choice;
    }

    @Override
    protected void initViews() {
        super.initViews();
        start_time = mRootView.findViewById(R.id.start_time);
        end_time = mRootView.findViewById(R.id.end_time);
        initStartTimeData();
        next_txt = mRootView.findViewById(R.id.next_txt);
        next_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String enddataStr = end_time.getText().toString();
                if (!TextUtils.isEmpty(enddataStr)) {
                    if (enddataStr.startsWith("20")) {
                        dataSelect = new DataSelect();
                        dataSelect.setStartdata(start_time.getText().toString());
                        dataSelect.setEnddata(enddataStr);
                        mPresenter.checkTime(packageBean.getCombId(), dataSelect.getStartdata(), dataSelect.getEnddata(), packageBean.getMembersSdId());
//                        EventBus.getDefault().post(new EventCenter<>(ArguConstant.PLACEORDER_GO_POWERSET));
                    } else {
                        ToastUtils.showShort("请完整选择时间");
                    }
                }
            }
        });
        //条件选择器
        OptionsPickerView pvOptions = new OptionsPickerBuilder(getContext(), new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                switch (dataSelectType) {
                    case 1:
                        if (monthList != null && monthList.size() != 0) {
                            if (isStartDataSelect) {

                                if (monthList.get(options1).get(options2) < 10) {
                                    start_time.setText(yearList.get(options1) + "-0" + monthList.get(options1).get(options2));
                                } else {
                                    start_time.setText(yearList.get(options1) + "-" + monthList.get(options1).get(options2));
                                }
                                userStartYear = yearList.get(options1);
                                userStartMonth = monthList.get(options1).get(options2);
                                initEndTimeData();
                            } else {
                                if (monthListEnd.get(options1).get(options2) < 10) {
                                    end_time.setText(yearListEnd.get(options1) + "-0" + monthListEnd.get(options1).get(options2));
                                } else {
                                    end_time.setText(yearListEnd.get(options1) + "-" + monthListEnd.get(options1).get(options2));
                                }
                            }
                        } else {
                            ToastUtils.showShort("用户可选月份为空");
                        }
                        break;
                    case 2:
                        if (seasonList != null && seasonList.size() != 0) {
                            if (isStartDataSelect) {
                                start_time.setText(yearList.get(options1) + "年第" + seasonList.get(options1).get(options2) + "季度");
                                userStartYear = yearList.get(options1);
                                userStartSeason = seasonList.get(options1).get(options2);
                                initEndTimeData();
                            } else {
                                end_time.setText(yearListEnd.get(options1) + "年第" + seasonList.get(options1).get(options2) + "季度");
                            }
                        } else {
                            ToastUtils.showShort("用户可选季度为空");
                        }
                        break;
                    case 3:
                        if (yearList != null && yearList.size() != 0) {
                            if (isStartDataSelect) {
                                start_time.setText(yearList.get(options1) + "");
                                userStartYear = yearList.get(options1);
                                initEndTimeData();
                            } else {
                                end_time.setText(yearListEnd.get(options1) + "");
                            }
                        } else {
                            ToastUtils.showShort("用户可选时间为空");
                        }
                        break;
                }
            }
        }).setDecorView((ViewGroup) getActivity().getWindow().getDecorView().findViewById(android.R.id.content)).build();

        LinearLayout select_startdata = mRootView.findViewById(R.id.select_startdata);
        LinearLayout select_enddata = mRootView.findViewById(R.id.select_enddata);
        select_startdata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                switch (dataSelectType) {
                    case 1:
                        if (yearList.size() != 0 && monthList.size() != 0) {
                            pvOptions.setPicker(yearList, monthList);
                            isStartDataSelect = true;
                            pvOptions.show();
                        } else {
                            ToastUtils.showShort("当前套餐暂无时间可选");
                        }
                        break;
                    case 2:
                        if (yearList.size() != 0 && seasonList.size() != 0) {
                            pvOptions.setPicker(yearList, seasonStrList);
                            isStartDataSelect = true;
                            pvOptions.show();
                        } else {
                            ToastUtils.showShort("当前套餐暂无时间可选");
                        }
                        break;
                    case 3:
                        if (yearList.size() != 0) {
                            pvOptions.setPicker(yearList);
                            isStartDataSelect = true;
                            pvOptions.show();
                        } else {
                            ToastUtils.showShort("当前套餐暂无时间可选");
                        }
                        break;
                }
            }
        });
        select_enddata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isStartDataSelect = false;
                if (yearListEnd.size() != 0) {
                    pvOptions.setPicker(yearListEnd, monthListEnd);
                    pvOptions.show();

                    switch (dataSelectType) {
                        case 1:
                            pvOptions.setPicker(yearListEnd, monthListEnd);
                            break;
                        case 2:
                            pvOptions.setPicker(yearListEnd, seasonStrListEnd);
                            break;
                        case 3:
                            pvOptions.setPicker(yearListEnd);
                            break;
                    }


                } else {
                    ToastUtils.showShort("请先选择开始时间");
                }

            }
        });

    }


    private void initStartTimeData() {
        chengeSelectData();

        int position = 0;
        for (int year = sysStartYear; year < sysEndYear + 1; year++) {
            yearList.add(year);
            monthList.add(new ArrayList<>());
            seasonList.add(new ArrayList<>());
            seasonStrList.add(new ArrayList<>());//季度比较特殊，多一组显示汉字数据，原数据做计算用

            for (int month = sysStartMonth; month < sysEndMonth + 1; month++) {
                monthList.get(position).add(month);
            }


            for (int season = sysStartSeason; season < sysEndSeason + 1; season++) {
                seasonList.get(position).add(season);
                seasonStrList.get(position).add("第" + season + "季度");
            }
            position++;
        }
    }


    private void initEndTimeData() {
        yearListEnd.clear();
        monthListEnd.clear();
        seasonListEnd.clear();
        seasonStrList.clear();
        int position = 0;

        for (int year = userStartYear; year < sysEndYear + 1; year++) {
            int mothlenth = 0;
            int seasonlenth = 0;
            yearListEnd.add(year);
            monthListEnd.add(new ArrayList<>());
            seasonListEnd.add(new ArrayList<>());
            seasonStrListEnd.add(new ArrayList<>());

            for (int month = userStartMonth; month < sysEndMonth + 1; month++) {
                if (dataSelectType == 1) {
                    if (!TextUtils.equals("不限", packageBean.getConstrMax().replace(" ", ""))) {
                        if (mothlenth == Integer.valueOf(packageBean.getConstrMax())) {
                            break;
                        }
                    }
                    mothlenth++;
                    monthListEnd.get(position).add(month);
                }


            }
            for (int season = userStartSeason; season < sysEndSeason + 1; season++) {
                if (dataSelectType == 2) {
                    if (!TextUtils.equals("不限", packageBean.getConstrMax())) {
                        if (seasonlenth == Integer.valueOf(packageBean.getConstrMax())) {
                            break;
                        }
                    }
                    seasonListEnd.get(position).add(season);

                    seasonStrListEnd.get(position).add("第" + season + "季度");
                }

            }
            position++;
        }
    }

    private void chengeSelectData() {
        //根据订单的时间范围以及周期单位，计算按月、按季度、按年的开始时间和结束时间

        Bundle bundle = getArguments();
        packageBean = (PackageBean) bundle.getSerializable("package");
        String startDataStr = DateUtils.getStrng_to_Times_ymd_3(packageBean.getEffectiveDate());
        String endDataStr = DateUtils.getStrng_to_Times_ymd_3(packageBean.getInvalidDate());
        String constrMinCyc = packageBean.getConstrMinCyc();

        String[] nowMouthStr = TimeUtils.getNowString().split("-");
        nowMouth = Integer.valueOf(nowMouthStr[1]);
        nowYear = Integer.valueOf(nowMouthStr[0]);
        String time1 = TimeUtils.getNowString();
        int nowDay = Integer.valueOf(time1.substring(8, 10));

        String[] startstr = startDataStr.split("-");
        sysStartYear = Integer.valueOf(startstr[0]);


        String[] endstr = endDataStr.split("-");
        sysEndYear = Integer.valueOf(endstr[0]);

        if (TextUtils.equals("01", constrMinCyc)) {
            //当计算周期以月为单位时
            dataSelectType = 1;
            sysStartMonth = Integer.valueOf(startstr[1]);
            sysEndMonth = Integer.valueOf(endstr[1]);


            //当套餐所给的周期中，分3*2+1种情况讨论。用以计算用户可选的开始月份
            // 1.当前月份小于周期开始月1个月。如果当前日大于15号则以周期月+1开始计数
            // 2.当前月份等于周期开始月，如果小于15号，以周期月+1开始计数；大于15号以周期月+2开始计数
            // 3.当前月份大于周期开始月，如果小于15号，以当前月+1开始技术；大于15号以当前月+2开始计数
            // 4.其余情况按照周期开始月份为计数
            if (nowYear == sysStartYear) {
                if (nowMouth + 1 == sysStartMonth) {
                    if (nowDay > 15) {
                        sysStartMonth++;
                    }
                }
                if (nowMouth == sysStartMonth) {
                    if (nowDay < 15) {
                        sysStartMonth++;
                    } else if (nowDay > 15) {
                        sysStartMonth = sysStartMonth + 2;
                    }
                }
                if (nowMouth > sysStartMonth) {
                    if (nowDay < 15) {
                        sysStartMonth = nowMouth + 1;
                    } else if (nowDay > 15) {
                        sysStartMonth = nowMouth + 2;
                    }
                }
            }
        } else if (TextUtils.equals("02", constrMinCyc)) {
            start_time.setText("请选择季度");
            end_time.setText("请选择季度");
            //当计算周期以季度为单位时
            dataSelectType = 2;
            if (nowMouth != 0) {
                //当套餐所给的周期中，月份小于当前时间月份。则以当前时间月来做开始时间
                if (nowMouth > sysStartMonth) {
                    sysStartMonth = nowMouth;
                }
            }
            if (sysStartMonth < 4) {
                //开始月份小于4，则季度开始值为2
                sysStartSeason = 2;
            } else if (sysStartMonth > 3 && sysStartMonth < 7) {
                //开始月份大于3 小于7，则季度开始值为3
                sysStartSeason = 3;
            } else if (sysStartMonth > 6 && sysStartMonth < 10) {
                //开始月份大于6 小于10，则季度开始值为4
                sysStartSeason = 4;
            } else {
                //开始月份大于10，则季度开始值为5  代表后续遍历直接跳出
                sysStartSeason = 5;
            }
        } else if (TextUtils.equals("03", constrMinCyc)) {
            dataSelectType = 3;
        } else if (TextUtils.equals("04", constrMinCyc)) {
            dataSelectType = 4;
            start_time.setClickable(false);
            start_time.setTextColor(0xff999999);
            end_time.setClickable(false);
            end_time.setTextColor(0xff999999);

            sysStartMonth = Integer.valueOf(startstr[1]);
            sysEndMonth = Integer.valueOf(endstr[1]);
            //当套餐所给的周期中，月份小于当前时间月份。则以当前时间月来做开始时间
            if (nowYear == sysStartYear) {
                if (nowMouth > sysStartMonth) {
                    sysStartMonth = nowMouth;
                }
            }

            if (sysStartMonth < 10) {
                start_time.setText(sysStartYear + "-0" + sysStartMonth);
            } else {
                start_time.setText(sysStartYear + "-" + sysStartMonth);
            }

            if (sysEndMonth < 10) {
                end_time.setText(sysEndYear + "-0" + sysEndMonth);
            } else {
                end_time.setText(sysEndYear + "-" + sysEndMonth);
            }


        }
        sysEndSeason = 4;
    }


    @Override
    protected OrderTimeChoicePresenter createPresenter() {
        return new OrderTimeChoicePresenter();
    }

    @Override
    public void queryTimeCheckSuccess(OrderCheckResutBean response) {
        if (response.getCode() == 200) {
            if (TextUtils.equals("1", response.getCheckState())) {
                if (onTimeSelectListener != null) {
                    onTimeSelectListener.timeSelect(dataSelect);
                }
            } else {
                ToastUtils.showLong(response.getMsg());
            }
        } else {
            ToastUtils.showLong(response.getMsg());
        }

    }

    @Override
    public void queryTimeCheckFailed() {

    }

    public interface OnTimeSelectListener {
        void timeSelect(DataSelect dataSelect);
    }

    public void setOnTimeSelectListener(OnTimeSelectListener timeSelectListener) {
        this.onTimeSelectListener = timeSelectListener;
    }

}